package com.example.demo.controller;


import com.example.demo.admin.dto.AddUser;
import com.example.demo.admin.dto.RegisterDto;
import com.example.demo.admin.dto.SearchDto;
import com.example.demo.admin.dto.UserDto;
import com.example.demo.admin.pojo.PageResult;
import com.example.demo.admin.pojo.Result;
import com.example.demo.admin.pojo.User;
import com.example.demo.admin.vo.DepartmentVO;
import com.example.demo.admin.vo.LoginVo;
import com.example.demo.admin.vo.StatisticsVo;
import com.example.demo.admin.vo.UserVo;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody RegisterDto registerDto) {
        log.info("参数{}", registerDto);
        userService.addUser(registerDto);
        return Result.success();
    }

    @PostMapping("/login")
    public Result login(@RequestBody RegisterDto registerDto) {
        log.info("参数{}", registerDto);
        LoginVo loginvo = userService.login(registerDto);
        return Result.success(loginvo);
    }

    //验证账号是否已存在
    @GetMapping("/onlyOne")
    public Result<User> exitUser(String username) {
        log.info("参数{}", username);
        userService.exitUser(username);
        return Result.success();
    }

    //获取员工详细信息
    @GetMapping("/getUserDetail")
    public Result<UserVo> getUserDetail() {
        log.info("用户信息");
        UserVo uservo = userService.getUserDetail();
        return Result.success(uservo);
    }

    //修改个人详细信息
    @PutMapping("/updatePrivateUser")
    public Result updatePrivateUser(@RequestBody UserDto userDto) {
        log.info("修改用户信息{}", userDto);
        userService.updateUser(userDto);
        return Result.success();
    }
   //更新头像
    @PatchMapping()
    public Result updateAvatar(@RequestBody Map<String,String> url){
        log.info("头像参数{}",url.get("image"));
        userService.updateAvatar(url.get("image"),url.get("username"));
        return Result.success();
    }

    //获取部门全部分类
    @GetMapping("/departments")
    public Result<List<DepartmentVO>> getDepartments() {
        log.info("获取部门分类信息");
        List<DepartmentVO> list = userService.getDepartments();
        return Result.success(list);
    }

    //获取职业全部分类
    @GetMapping("/jobs")
    public Result<List<DepartmentVO>> getJobs() {
        log.info("修改用户信息");
        List<DepartmentVO> list = userService.getJobs();
        return Result.success(list);
    }

    //分页查询员工数据
    @GetMapping("/pageUser")
    public Result<PageResult> getAllUser(SearchDto searchDto) {
        log.info("分页查询数据{}", searchDto);
        PageResult pageResult = userService.getAllUser(searchDto);
        return Result.success(pageResult);
    }


    //添加员工
    @PostMapping("/addUser")
    public Result addUser(@RequestBody AddUser addUser) {
        log.info("添加员工数据{}", addUser);
        userService.saveUser(addUser);
        return Result.success();
    }

    //删除员工
    @DeleteMapping("/delete")
    public Result deleteUser(String username) {
        log.info("删除用户参数{}", username);
        userService.deleteUser(username);
        return Result.success();
    }

    //修改员工详细信息
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody UserDto userDto) {
        log.info("修改用户信息{}", userDto);
        userService.updateUser(userDto);
        return Result.success();
    }

    //重置密码
    @GetMapping("/reset")
    public Result resetPassword(String username) {
        userService.resetPassword(username);
        return Result.success();
    }

    //获取每个月新增员工数据
    @GetMapping("/statistics")
    public Result<StatisticsVo> UserStatistics() {
        StatisticsVo statisticsVo = userService.UserStatistics();
        return Result.success(statisticsVo);
    }
}
